import Mock from 'mockjs2'
import { builder, getBody } from '../util'
import moment from 'moment'
const DataSet = require('@antv/data-set')

const getDeviceCountStatistics = (options) => {
  const body = getBody(options)

  return builder({
    'all': 2000,
    'run': 1995,
    'hitch': 100,
    'alarm': 83,
    'offline': 5
  })
}

const getAlarmDeviceCountStatistics = (options) => {
  const body = getBody(options)

  const type = body.type
  let str = '时'
  if(type == '2') str = '日'
  if(type == '3') str = '月'

  const barData = []
  for (let i = 0; i < 12; i += 1) {
    barData.push({
      x: `${i + 1} ${str}`,
      y: Math.floor(Math.random() * 1000) + 200
    })
  }

  return builder(barData)
}

const getDeviceAlarmEvent = (option) => {
  const alarmEvent = []
  const otherEvent = []
  for(let i =0;i<5;i++){
    alarmEvent.push({
      id:i,
      createdAt:moment().format('MM/DD HH:mm'),
      name:`NO-IOT-${i + 1}`,
      remark: '护栏侧翻倾倒'
    })
    otherEvent.push({
      id:i,
      createdAt:moment().format('MM/DD HH:mm'),
      name:`NO-IOT-${i + 1}`,
      remark: '未收到信号'
    })
  }
  const result = {
    alarmEvent,otherEvent
  }
  return builder(result)
}

const getDeviceCountByTime = (option) => {
  const deviceCount = []
  deviceCount.push({
    item:'正常',
    count: 1957
  })
  deviceCount.push({
    item:'离线',
    count: 13
  })
  deviceCount.push({
    item:'故障',
    count: 10
  })
  deviceCount.push({
    item:'告警',
    count: 20
  })

  const dv = new DataSet.View().source(deviceCount)
  dv.transform({
    type: 'percent',
    field: 'count',
    dimension: 'item',
    as: 'percent'
  })
  const pieData = dv.rows

  return builder(deviceCount)
}

const deviceList = (options) => {
  const parameters = getBody(options)

  const totalCount = 5302
  const result = []
  const pageNo = parseInt(parameters.pageNo)
  const pageSize = parseInt(parameters.pageSize)
  const totalPage = Math.ceil(totalCount / pageSize)
  const key = (pageNo - 1) * pageSize
  const next = (pageNo >= totalPage ? (totalCount % pageSize) : pageSize) + 1

  for (let i = 1; i < next; i++) {
    const tmpKey = key + i
    result.push({
      key: tmpKey,
      id: tmpKey,
      no: 'No ' + tmpKey,
      devModel: 'LB120A',
      devNo: Mock.mock('@integer(1, 999)'),
      status: Mock.mock('@integer(0, 3)'),
      updatedAt: Mock.mock('@datetime'),
      editable: false
    })
  }

  return builder({
    pageSize: pageSize,
    pageNo: pageNo,
    totalCount: totalCount,
    totalPage: totalPage,
    data: result
  })
}

Mock.mock(/\/device\/getDeviceList/, 'post', deviceList)
Mock.mock(/\/device\/getDeviceCount/, 'get', getDeviceCountStatistics)
Mock.mock(/\/device\/getAlarmDeviceCount/, 'post', getAlarmDeviceCountStatistics)
Mock.mock(/\/device\/getDeviceAlarmEvent/, 'post', getDeviceAlarmEvent)
Mock.mock(/\/device\/getDeviceCountByTime/, 'post', getDeviceCountByTime)